<?php

namespace app\common\model;
use think\Model;

class MyPackage extends Model
{
    public function getTypeTextAttr($value,$data)
    {
        $status = [1=>'礼物',2=>'道具卡'];
        return $status[$data['type']];
    }


    public static function getRows($map = [])
    {
        return self::alias('p')
            ->join('gift g','g.gift_id=p.key','LEFT')
            ->where($map)
            ->field('g.gift_name,g.img,g.thumbnail,p.num,p.type,p.create_time,p.key as gift_id,g.price,g.type as gift_type')
            ->order('p.create_time desc')
            ->select();
    }

    public  function getOne($map = [])
    {
        $v =   self::alias('p')
            ->join('draw d','d.id=p.key','LEFT')
            ->join('users u','u.user_id=p.user_id','LEFT')
            ->where($map)
            ->field('p.*,d.name,d.img,u.nick_name,u.uuid,d.price')
            ->find();
        switch ($v['type']){
            case 1://礼物
                $v['name'] = getGiftName($v['key']);
                $v['img'] = getGiftImg($v['key']);
                $v['price'] = getGiftPrice($v['key']);
                break;
            case 2://道具卡 预留

                break;
            case 3://装饰 预留
                break;
            case 4: //铜宝箱
                if (\app\api\controller\Base::getExplain('boxAct') === 'true'){
                    $v['num']  = 999;
                }
                $v['name'] = '铜宝箱';
                $v['img']  = 'http://file.mengpaxing.com/69800201904191404029202.png';
                break;
            case 5: //银宝箱
                $v['name'] = '银宝箱';
                if (\app\api\controller\Base::getExplain('boxAct') === 'true'){
                    $v['num']  = 999;
                }
                $v['img']  = 'http://file.mengpaxing.com/b4317201904191404286835.png';
                break;
            case 6: //金宝箱
                $v['name'] = '金宝箱';
                if (\app\api\controller\Base::getExplain('boxAct') === 'true'){
                    $v['num']  = 999;
                }
                $v['img']  = 'http://file.mengpaxing.com/4671f201904191404395945.png';
                break;
            default:
                $v['name'] = '';
                $v['img']  = '';
                break;
        }

        return $v;


    }
    public  function getRows1($map = [])
    {
        return  self::alias('p')
            ->join('draw d','d.id=p.key','LEFT')
            ->join('users u','u.user_id=p.user_id','LEFT')
            ->where($map)
            ->field('p.*,d.name,d.img,u.nick_name,u.uuid,d.price')
            ->order('p.type desc')
            ->paginate()->each(function ($v){
                switch ($v['type']){
                    case 1://礼物
                        $v['name'] = getGiftName($v['key']);
                        $v['img'] = getGiftImg($v['key']);
                        $v['price'] = getGiftPrice($v['key']);
                        break;
                    case 2://道具卡 预留

                        break;
                    case 3://装饰 预留
                        break;
                    case 4: //铜宝箱
                        if (\app\api\controller\Base::getExplain('boxAct') === 'true'){
                            $v['num']  = 999;
                        }
                        $v['name'] = '铜宝箱';
                        $v['img']  = 'http://file.mengpaxing.com/69800201904191404029202.png';
                        break;
                    case 5: //银宝箱
                        $v['name'] = '银宝箱';
                        if (\app\api\controller\Base::getExplain('boxAct') === 'true'){
                            $v['num']  = 999;
                        }
                        $v['img']  = 'http://file.mengpaxing.com/b4317201904191404286835.png';
                        break;
                    case 6: //金宝箱
                        $v['name'] = '金宝箱';
                        if (\app\api\controller\Base::getExplain('boxAct') === 'true'){
                            $v['num']  = 999;
                        }
                        $v['img']  = 'http://file.mengpaxing.com/4671f201904191404395945.png';
                        break;
                    default:
                        $v['name'] = '';
                        $v['img']  = '';
                        break;
                }
            });


    }

    public static function getCard($map = [])
    {
        $rows =  self::alias('p')
            ->join('draw d','d.id=p.key','LEFT')
            ->where($map)
            ->field('d.name as gift_name,d.img,p.num,p.type,p.create_time,p.key as gift_id,d.price')
            ->order('p.create_time desc')
            ->select();

        foreach ($rows as $k => $v){
            if ($v['type'] == 1){
                //礼物
                $rows[$k]['price'] = getGiftPrice($v['key']);
                $rows[$k]['gift_type'] = getGiftType($v['key']);
            }else{
                $rows[$k]['gift_type'] = 1;
            }
        }
        return $rows;
    }


}